#include <xp/bas/arr.h>
#include <xp/bas/stdio.h>

xp_ssize_t print_datum (xp_arr_t* arr, xp_size_t index, void* datum)
{
	xp_printf (XP_TEXT("%lu: %lld\n"), 
		(unsigned long)index, *(xp_int64_t*)datum);
	return 1;
}

int xp_main (void)
{
	xp_int64_t a[] = { 3, 4, 10, 10, 30, 193, 10};
	xp_int64_t xx;
	xp_arr_t* arr;
	xp_size_t i, index, count;

	arr = xp_arr_open (XP_NULL, 4, xp_sizeof(xp_int64_t), XP_NULL);
	if (arr == XP_NULL) {
		xp_printf (XP_TEXT("can't allocate arr\n"));
		return -1;
	}

	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_printf (XP_TEXT("----------------------------\n"));

	
	for (i = 0; i < xp_countof(a); i++) {
		xp_printf (XP_TEXT("adding %ld\n"), (long)a[i]);
		if (xp_arr_adddatum (arr, &a[i]) == XP_NULL) {
			xp_printf (XP_TEXT("can't add elment to arr\n"));
			xp_arr_close (arr);
			return -1;
		}
	}
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xp_printf (XP_TEXT("sort decending...\n"));
	xp_arr_sort (arr, 0, arr->size, XP_ARR_SORT_DESCENDING);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xp_printf (XP_TEXT("sort ascending... \n"));
	xp_arr_sort (arr, 0, arr->size, XP_ARR_SORT_ASCENDING);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xp_printf (XP_TEXT("sort decending (1/5)...\n"));
	xp_arr_sort (arr, 1, 5, XP_ARR_SORT_DESCENDING);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xx = 50;
	index = 6;
	count = 5;
	xp_printf (XP_TEXT("filling %lu elements from %lu with %lld\n"), 
		count, index, xx);
	xp_arr_fill (arr, index, count, &xx);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xx = 100;
	index = 20;
	count = 5;
	xp_printf (XP_TEXT("filling %lu elements from %lu with %lld\n"), 
		count, index, xx);
	xp_arr_fill (arr, index, count, &xx);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xp_printf (XP_TEXT("clearing array\n"));
	xp_arr_clear (arr);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	xx = 12345;
	index = 10;
	xp_printf (XP_TEXT("putting %lld at %lu\n"), xx, (unsigned long)index);
	xp_arr_putdatum (arr, index, &xx);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	index = 0; count = 10;
	xp_printf (XP_TEXT("removing %lu elements from %lu\n"), 
		(unsigned long)count, (unsigned long)index);
	xp_arr_remrange (arr, index, count);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	index = 30; count = 10;
	xp_printf (XP_TEXT("removing %lu elements from %lu\n"), 
		(unsigned long)count, (unsigned long)index);
	xp_arr_remrange (arr, index, count);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	index = 0; count = 0;
	xp_printf (XP_TEXT("removing %lu elements from %lu\n"), 
		(unsigned long)count, (unsigned long)index);
	xp_arr_remrange (arr, index, count);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));

	index = 0; count = 1;
	xp_printf (XP_TEXT("removing %lu elements from %lu\n"), 
		(unsigned long)count, (unsigned long)index);
	xp_arr_remrange (arr, index, count);
	xp_printf (XP_TEXT("[capacity: %u, size: %u]\n"), 
		(unsigned int)arr->capa, (unsigned int)arr->size);
	xp_arr_walk (arr, 0, print_datum);
	xp_printf (XP_TEXT("----------------------------\n"));
	
	xp_arr_close (arr);

	return 0;
}
